package top.callname.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import top.callname.bean.AdminBean;
import top.callname.bean.ConstanstBean;
import top.callname.service.AdminService;
import top.callname.service.impl.AdminServiceImpl;

@WebServlet("/admin/adminManager")
public class AdminManagerServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(AdminManagerServlet.class);
	private AdminService adminService = new AdminServiceImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String method = request.getParameter("method");
		
		if("login".equals(method)){
			
			String uname = request.getParameter("uname");
			String upwd = request.getParameter("upwd");
			AdminBean admin = adminService.adminLogin(uname, upwd);
	
				if(ConstanstBean.RSP_SUCCESS.equals(admin.getRspCode())){
					request.getSession().setAttribute("adminInfo", admin);
					if(ConstanstBean.ROLE_ADMIN == admin.getRole()){
						logger.info("教师 "+uname+" 成功登录到后台！");
						response.sendRedirect("teacher"); 
					}else{
						logger.info("管理员 "+uname+" 成功登录到后台！");
						response.sendRedirect("home"); 
					}
					
				}else{
					request.setAttribute("loginError", admin.getRspDesc());
					request.getRequestDispatcher("login.jsp").forward(request,response);  
				}
			
		}else if ("logout".equals(method)){
			
			AdminBean admin = (AdminBean) request.getSession().getAttribute("adminInfo");
			logger.info("管理员 "+admin.getUsername()+" 成功退出后台！");
			request.getSession().removeAttribute("adminInfo");
			response.sendRedirect("login.jsp"); 
			
		}else if ("reg".equals(method)){
			String username = request.getParameter("username");
			String fullname = request.getParameter("fullname");
			String passwd = request.getParameter("passwd");
			String passwd2 = request.getParameter("passwd2");
			AdminBean bean = new AdminBean();
			bean.setFullname(fullname);
			bean.setPasswd2(passwd2);
			bean.setPasswd(passwd);
			bean.setUsername(username);
			bean = adminService.adminRegist(bean);
			logger.info("用户注册："+bean.toString());
			if(ConstanstBean.RSP_SUCCESS.equals(bean.getRspCode())){
				request.setAttribute("regSuccess", bean.getRspDesc());
				request.getRequestDispatcher("login.jsp").forward(request,response); 
			}else{
				request.setAttribute("regError", bean.getRspDesc());
				request.setAttribute("admin", bean);
				request.getRequestDispatcher("regist.jsp").forward(request,response);  
			}
		}else{
			response.sendRedirect("login.jsp"); 
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
